package com.superpeer.baseproject.utils;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularUtils {
    private static String reg = "^[\\u4e00-\\u9fa5_a-zA-Z0-9]+$";//中文字母数字
    private static String reg2 = "[[ ]&'\"<>/\\\\]";//特殊符号
    private static String reg3 = "[&'\"<>/\\\\]";//特殊符号不包含空格
    private static String reg4 = "[&'\"<>\\\\]";//特殊符号不包含空格,斜杠

    public static boolean isChineseEnglishNumber(String text){
        Pattern p = Pattern.compile(reg);
        Matcher matcher = p.matcher(text);
        if(!matcher.matches()){
            return false;
        }else{
            return true;
        }
    }

    public static boolean isHaveSpecialSymbols(String text){
        Pattern p = Pattern.compile(reg4);
        Matcher matcher = p.matcher(text);
        if(matcher.find()){
            return true;
        }else{
            return false;
        }
    }

    public static boolean isEmail(String email){
        String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        boolean flag = matcher.matches();
        return flag;
    }

    public static boolean textUnderMax(String str, int maxLen) {
        if (TextUtils.isEmpty(str)) {
            return true;
        }
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            char item = str.charAt(i);
            if (item < 128) {
                count = count + 1;
            } else {
                count = count + 2;
            }
        }
        if (count <= maxLen) {
            return true;
        } else {
            return false;
        }
    }
}
